//
//  JDSendingViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/6/24.
//

import UIKit
import JXSegmentedView
/// 已接单
class JDSendingViewController: JDBaseViewController {
    
    var baseVC: UIViewController?
    var mainTableView: UITableView?
    
    var isFirstLoad: Bool = true
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.isFirstLoad == false {
//            self.getNetData(true)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .commonColor()
        self.setupUIElements()
        self.getNetData()
        self.isFirstLoad = false
        // Do any additional setup after loading the view.
    }
    
    func setupUIElements() {
        
        self.mainTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.mainTableView!)
        self.mainTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.view)
            make.bottom.equalTo(self.view)
        })
        self.mainTableView?.delegate = self
        self.mainTableView?.dataSource = self
        self.mainTableView?.register(JDOrderTableViewCell.self, forCellReuseIdentifier: "myCell")
        self.mainTableView?.register(JDOrderListReleaseCell.self, forCellReuseIdentifier: "releaseCell")
        self.mainTableView?.backgroundColor = self.commonColor
        self.mainTableView?.separatorStyle = .none
    }
    
    func getNetData() {
            
        var params = [String: Any]()
//        params["onlineSign"] = JDLoginModel.default.onlineSign
        // 订单状态
//        "" :全部, 1：等付款，2：已付款待接单，3：接单待发货，
//        4：已发货待收，5：确认收货，6：订单取消,7：拒接订单
        params["orderState"] = "3"
        params["pageNumber"] = self.currentPage
        params["pageSize"] = 10
        
        JDNetWorkRequest(.userOrderList(params)) { responseStr in
            print("返回数据:\(responseStr)")
            // 接口返回 returnCode == 00，认为是正常，但是，因为没有数据，所以，无法查看具体数据信息
        } failure: { error in
            printLog("错误:\(error)")
        } error: { errorCode in
            printLog("错误code:\(errorCode)")
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDSendingViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as? JDOrderTableViewCell
        cell?.orderType = .sending
        cell?.refreshData()
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 100
        return UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        printLog("代付款->点击:\(indexPath.row)")
        
        let orderDetailVC = JDOrderDetailViewController()
        self.baseVC?.hidesBottomBarPushViewController(orderDetailVC)
        
    }
    
}

extension JDSendingViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return self.view
    }
}
